<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="产线" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="line">
              <j-dict-select-tag type="list" v-model="model.line" dictCode="mes_line_cfg,LINE,LINE"
                                 placeholder="请选择产线" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="开始时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="startTime">
              <j-date placeholder="请选择开始时间" v-model="model.startTime" :show-time="true"
                      date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" @change="dateChange"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="结束时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="endTime">
              <j-date placeholder="请选择结束时间" v-model="model.endTime" :show-time="true"
                      date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" @change="dateChange"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="时长(S)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="hours">
              <a-input-number v-model="model.hours" placeholder="请输入时长" style="width: 100%" />
            </a-form-model-item>
          </a-col>
<!--          <a-col :span="12">-->
<!--            <a-form-model-item label="班次日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="shiftDate">-->
<!--              <j-date placeholder="请选择班次日期" v-model="model.shiftDate" style="width: 100%" />-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
<!--          <a-col :span="12">-->
<!--            <a-form-model-item label="班次" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="shift">-->
<!--              <a-input v-model="model.shift" placeholder="请输入班次"></a-input>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
          <a-col :span="24">
            <a-form-model-item label="原因" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="reason">
              <a-input v-model="model.reason" placeholder="请输入原因"></a-input>
            </a-form-model-item>
          </a-col>
<!--          <a-col :span="12">-->
<!--            <a-form-model-item label="操作人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="user">-->
<!--              <a-input v-model="model.user" placeholder="请输入操作人"></a-input>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-input v-model="model.remark" placeholder="请输入备注"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
import { getDateDiff } from 'xe-utils/methods'

export default {
  name: 'MesHaltTimeForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      isEdit: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        recId: [
          { required: true, message: '请输入主键ID!' }
        ],
        line: [
          { required: true, message: '请输入产线!' }
        ]
      },
      url: {
        add: '/prod/mesHaltTime/add',
        edit: '/prod/mesHaltTime/edit',
        queryById: '/prod/mesHaltTime/queryById'
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    dateChange(){
      console.log('日期改变')
      if (this.model.startTime&&this.model.endTime){
        var d1 = new Date(this.model.startTime);
        var d2 = new Date(this.model.endTime);
        let dd = (d2 - d1)/1000
        //let dd = getDateDiff('S',this.model.startTime,this.model.endTime)
        console.log('日期差'+dd)
        console.log(dd)
        this.model.hours = dd
      }
    },
    add() {
      this.isEdit = false
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      if (JSON.stringify(record) !== '{}') {
        this.isEdit = true
      }
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.isEdit) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
          })
        }

      })
    }
  }
}
</script>